﻿package bdz.projects.lavender.doi.ui
{
	import flash.display.MovieClip;
	import flash.utils.*;
	import gs.*;
	import gs.easing.*;
	import bdz.projects.lavender.doi.utils.DOIAudio;
	import org.papervision3d.objects.DisplayObject3D;
	/**
	 * ...
	 * @author ...
	 */
	public dynamic class PaperMenu extends DisplayObject3D 
	{	
		protected var _items:Array;
		protected var _papers:Array;
		protected var _baseItemWidth:Number;
		protected var _baseItemHeight:Number;
		protected var _renderType:String;
		public var _padding:Number = 2;
		public var labelZ:Number = -40;
		protected var _ixoffset:Number = 0;
		private var _ishow:Boolean;
		private var _random:Boolean;
		public var baseTransparency:Number = 1;
		private var _audio:DOIAudio;
		public function PaperMenu(itms:Array, rtype:String, lz:Number = -40, ixoffset:Number = 0, pad:Number = 2, ishow:Boolean = true, randomize:Boolean = false ) {
			
			super("papermenu");
			_random = randomize;
			_padding = pad;
			_ixoffset = ixoffset;
			labelZ = lz;
			_items = itms;
			_renderType = rtype;
			_ishow = ishow;
			_audio = DOIAudio.getInstance();
			generateMenu();
			
		}
		protected function generateMenu():void {
			_papers = new Array();
			
			for each(var obj:* in _items) {
				var bobj:* = getDefinitionByName(obj.baseRenderer);
				var lobj:* = getDefinitionByName(obj.labelRenderer);
				var baseclip:MovieClip = new bobj();
				var labelclip:MovieClip = new lobj();
				var pref:PaperMenuItem = new PaperMenuItem(baseclip , labelclip, obj, false, _ixoffset, _ishow);
				pref.baseTransparency = baseTransparency;
				_baseItemWidth = pref.rendererWidth;
				_baseItemHeight = pref.rendererHeight;
				_papers.push(pref);
				addChild(pref);
				baseclip = null;
				labelclip = null;
				bobj = null;
				lobj = null;
				
			}
			
			organizeContent();
		}
		protected function layoutGrid():void {
			var sy:Number = _baseItemHeight / 2;
			var sx:Number = -_baseItemWidth / 2;
			var cols:Number = _items.length / 2;
			var count:Number = 0;
			for (var i:Number = 0; i < 2; i++) {
				for (var k:Number = 0; k < cols; k++) {
					_papers[count].x = sx;
					_papers[count].y = sy;
					sx += _baseItemWidth+_padding;
					count++;
					if (_random) {
						_papers[count].origx = _papers[count].x;
						_papers[count].origy = _papers[count].y;
						_papers[count].x = (Math.random() * 400) - 200;
						_papers[count].y = (Math.random() * 400) - 200;
					}
					
				}
				sx =  -_baseItemWidth / 2;
				sy -= _baseItemHeight + _padding;
			}
			
		}
		
		protected function layoutHorizontal():void {
			var sx:Number = -(((_baseItemWidth+_padding)*_papers.length) / 2)+_baseItemWidth/2;
			for each(var pitem:PaperMenuItem in _papers) {
				pitem.x = sx;
				sx += pitem.rendererWidth + _padding;
			}
		}
		private function organizeContent():void {
			switch(_renderType) {
				case "grid":
					layoutGrid();
				break;
				case "horizontal":
					layoutHorizontal();
				break;
			}
		}
		public function cleanUp() {
			for each(var pmi:PaperMenuItem in _papers) {
				pmi.cleanUp();
				removeChild(pmi);
			}
			_papers = null;
			try{
				parent.removeChild(this);
			}catch (e:Error) {
				
			}
			
			
		}
		public function show(indx:Number):void {
			 _audio.playEffect(ting); 
			(_papers[indx] as PaperMenuItem).show();
		}
		public function showAll(def:Number = -1):void{
			var delay:Number = 0;
			var count:Number = 0;
			for each(var pmi:PaperMenuItem in _papers){
				if(count == def){
					pmi.show(delay, -40);
					pmi.select();	
				}else{
					pmi.show(delay);
				}
				delay += .1;
				count++;
			}
		}
		public function hideAll():void{
			var delay:Number = 0;
			for each(var pmi:PaperMenuItem in _papers){
				pmi.hide(delay);
				delay += .1;
			}
		}
		public function deactivate():void {
			for each(var pmi:PaperMenuItem in _papers) {
				pmi.deactivate();
			}
		}
		public function activate():void {
			for each(var pmi:PaperMenuItem in _papers) {
				pmi.activate();
			}
		}
		public function get items():Array {
			return _items;
		}
		public function get planes():Array {
			return _papers;
		}
		
	}
	
}